"""
FastAPI 主应用入口
"""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
# from fastapi.staticfiles import StaticFiles
# from pathlib import Path
from app.database import engine, Base
from app.routers import works, favorites, users, gallery
# from app.routers import upload  # 暂时禁用，等接入OSS后再启用
# from app.config import settings

# 创建数据库表
Base.metadata.create_all(bind=engine)

# 创建FastAPI应用
app = FastAPI(
    title="云图知道小程序后端API",
    description="艺术工坊小程序后端接口",
    version="1.0.0"
)

# 配置CORS，允许小程序跨域请求
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 生产环境应该设置具体的小程序域名
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册路由
app.include_router(works.router, prefix="/api", tags=["作品"])
app.include_router(gallery.router, prefix="/api", tags=["客片"])
app.include_router(favorites.router, prefix="/api", tags=["收藏"])
app.include_router(users.router, prefix="/api", tags=["用户"])

# 上传功能暂时禁用，等接入OSS后再启用
# 确保上传目录存在
# upload_dir = Path(settings.UPLOAD_DIR)
# upload_dir.mkdir(parents=True, exist_ok=True)
# app.include_router(upload.router, prefix="/api", tags=["上传"])
# app.mount("/uploads", StaticFiles(directory=str(upload_dir)), name="uploads")


@app.get("/")
async def root():
    """根路径，健康检查"""
    return {
        "message": "云图知道小程序后端API",
        "status": "running",
        "version": "1.0.0"
    }


@app.get("/health")
async def health():
    """健康检查接口"""
    return {"status": "healthy"}

